﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Top.Web;
using Top.Web.Api;
using Top.Web.Api.Filters;
using Top.Core;
using Top.PointStore.Business;
using Top.PointStore.Web.Filters;
//using Top.PointStore.Filters;

namespace Top.PointStore.Controllers
{
    /// <summary>
    /// 积分商品
    /// </summary>
    [Provider]
    public class PointProductController : ApiBase
    {
        /// <summary>得到一条记录
        /// </summary>
        /// <param name="kid"></param>
        //[Login]
        public object Get(int kid)
        {
            return PointProductManager.Get(kid);
        }

        /// <summary> 修改商品
        /// </summary>
        /// <param name="productName">商品名称</param>
        /// <param name="description">商品描述</param>
        /// <param name="pointNum">兑换积分</param>
        /// <param name="status">处理类型</param>
        /// <param name="kid"></param>
        /// <param name="sort"></param>
        /// <param name="categoryKid"></param>
        /// <param name="picUrl"></param>
        /// <param name="source"></param>
        [Login]
        public async Task<object> UpdateField(string productName, string description, int pointNum, int status, int kid, int sort, int categoryKid, string picUrl, int inventoryNum, int stockNum, double productPrice, string source = "")
        {
            var model = new PointProductExpand();
            model.Kid = kid;
            model.F_ProductName = productName;
            model.F_Description = description;
            model.F_PicUrl = picUrl;
            model.F_PointNum = pointNum;
            model.F_Status = status;
            model.F_Source = source;
            model.F_Sort = sort;
            model.F_CategoryKid = categoryKid;
            model.F_StockNum = stockNum;
            model.F_ProductPrice = productPrice;
            model.F_InventoryNum = inventoryNum;
            var res = await PointProductManager.UpdateField(model);
            switch (res)
            {
                case 0:
                    throw new WebApiException("更新失败");
                case -1:
                    throw new WebApiException("商品id有误");
                case -2:
                    throw new WebApiException("商品名称不能为空");
                case -3:
                    throw new WebApiException("详细说明不能为空");
                case -4:
                    throw new WebApiException("积分需为正数");
                case -5:
                    throw new WebApiException("自动处理时来源不能为空");
                default:
                    break;
            }

            await SystemManager.AddLog(User.CrmUserID, "PointProductAddedEdit", "操作人" + User.Name + "编辑了商品编号(" + model.Kid + ")一条记录");
            return res;
        }


        /// <summary>加入仓库
        /// </summary>
        /// <param name="typeId">产品上架类型：0，加入仓库；1、立即上架</param>
        /// <param name="categoryKid">商品分类编号</param>
        /// <param name="productName">商品名称</param>
        /// <param name="pointNum">积分</param>
        /// <param name="inventoryNum">商品库存</param>
        /// <param name="isExchange">是否开启规则</param>
        /// <param name="status">处理类型</param>
        /// <param name="source">来源</param>
        /// <param name="exchangeType">兑换类型</param>
        /// <param name="userExchangeNum">用户兑换次数</param>
        /// <param name="code">商品编码</param>
        /// <param name="exchangeNum">每日兑换数量</param>
        /// <param name="exchangeBeginTime">每日兑换开始时间</param>
        /// <param name="exchangeEndTime">每日兑换结束时间</param>
        /// <param name="sort">排序</param>
        /// <param name="kid">商品编号</param>
        /// <param name="picUrl">商品主图片</param>
        /// <param name="description">商品说明</param>
        /// <param name="productPic">产品图片</param>
        /// <param name="ruleToCity">规则与城市</param>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="isMeskill">是否参与秒杀 0：不参与 ； 1：参与秒杀</param>
        /// <returns></returns>
        [Login]
        public async Task<int> Save(int typeId, int categoryKid, string productName, int pointNum, int inventoryNum, int isExchange, int status, int exchangeType, int userExchangeNum,
            string code, string picUrl, string description, string ruleToCity, DateTime? beginTime, DateTime? endTime, int sort, int kid = 0, string productPic = "", string exchangeBeginTime = "",
            string exchangeEndTime = "", int exchangeNum = 1, string source = "", int isMeskill = 0, int ruleKid = 0, int stockNum = 0, double productPrice = 0)
        {
            PointProductExpand model = new PointProductExpand();
            if (isExchange == 1)
            {
                if (!string.IsNullOrWhiteSpace(exchangeBeginTime) && !string.IsNullOrWhiteSpace(exchangeEndTime))
                {
                    string[] start = exchangeBeginTime.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] end = exchangeEndTime.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    if (Convert.ToInt32(start[0]) > Convert.ToInt32(end[0]))
                    {
                        throw new WebApiException("每日限兑时间 开始时间不允许大于结束时间！");
                    }
                    if (Convert.ToInt32(start[0]) == Convert.ToInt32(end[0]))
                    {
                        if (Convert.ToInt32(start[1]) > Convert.ToInt32(end[1]))
                        {
                            throw new WebApiException("每日限兑时间 开始时间不允许大于结束时间！");
                        }
                    }
                }
                model.F_ExchangeNum = exchangeNum;
                model.F_ExchangeBeginTime = exchangeBeginTime;
                model.F_ExchangeEndTime = exchangeEndTime;
            }
            //积分商品
            model.Kid = kid;
            model.F_ProductName = productName;
            model.F_CategoryKid = categoryKid;
            model.F_PicUrl = picUrl;
            model.F_Description = description;
            model.F_Code = code;
            //库存
            model.F_StockNum = stockNum;
            //单价
            model.F_ProductPrice = productPrice;
            model.F_InventoryNum = inventoryNum;
            model.F_Source = source;
            //产品图片
            model.ProductPic = productPic;
            //规则与城市
            model.RuleToCity = ruleToCity;
            #region 积分规则
            model.F_BeginTime = beginTime ?? DateTime.Now;
            model.F_EndTime = endTime ?? DateTime.Now.AddDays(30);
            model.F_EndTime = Convert.ToDateTime(model.F_EndTime).AddDays(1);
            model.F_PointNum = pointNum;
            model.F_Disabled = 1;
            model.F_Status = status;
            model.F_ExchangeType = exchangeType;
            model.F_UserExchangeNum = userExchangeNum;
            model.F_IsExchange = isExchange;
            model.F_IsMeskill = isMeskill;
            model.ruleKid = ruleKid;

            #endregion

            model.F_Sort = sort;
            model.TypeId = typeId; //产品上架类型：0，加入仓库；1、立即上架
            var result = 0;
            if (model.Kid > 0)
            {
                model.F_UpdatedTime = DateTime.Now;
                result = await PointProductManager.Update(model);
                if (result > 0)
                {
                    await SystemManager.AddLog(User.CrmUserID, "PointProductEdit", "操作人" + User.Name + "编辑了商品编号(" + model.Kid + ")一条记录");
                }
            }
            else
            {
                model.F_IsDelete = 0;
                model.F_CreatedTime = DateTime.Now;
                model.F_UpdatedTime = DateTime.Now;

                result = await PointProductManager.Add(model);

                if (result > 0)
                {
                    await SystemManager.AddLog(User.CrmUserID, "PointProductAdd", "操作人" + User.Name + "添加了商品编号(" + result + ")一条记录");
                }
            }
            switch (result)
            {
                case 0:
                    throw new WebApiException("操作失败");
                    break;
                case -1:
                    throw new WebApiException("商品分类编号不能为空");
                    break;
                case -2:
                    throw new WebApiException("商品名称不能为空");
                    break;
                case -3:
                    throw new WebApiException("兑换积分不能为空小于0");
                    break;
                case -4:
                    throw new WebApiException("商品库存不能为空");
                    break;
                case -5:
                    throw new WebApiException("是否开启规则不能为空");
                    break;
                case -6:
                    throw new WebApiException("处理类型不能为空");
                    break;
                case -7:
                    throw new WebApiException("自动处理时来源不能为空");
                    break;
                case -8:
                    throw new WebApiException("兑换类型不能为空");
                    break;
                case -9:
                    throw new WebApiException("用户兑换次数不能为空");
                    break;
                case -10:
                    throw new WebApiException("商品编码不能含有特殊字符");
                    break;
                case -11:
                    throw new WebApiException("每日兑换数量不能为空");
                    break;
                case -12:
                    throw new WebApiException("每日兑换开始日期不能为空");
                    break;
                case -13:
                    throw new WebApiException("每日兑换结束日期不能为空");
                    break;
                case -14:
                    throw new WebApiException("商品id有误");
                    break;
                default:
                    break;
            }
            return result;
        }





    }
}
